package com.google.code.mamule.price;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.google.code.mamule.integration.Endpoint;
import com.google.code.mamule.integration.Message;
import com.google.code.mamule.mail.Mail;
import com.google.code.mamule.mail.MailRepository;
import com.google.code.mamule.order.Order;
import com.google.code.mamule.order.OrderItems;
import com.google.code.mamule.order.OrderProcessor;
import com.google.code.mamule.order.OrderRepository;
import com.google.code.mamule.user.ActiveUser;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;

@Component
@Path("/price")
@Produces(MediaType.APPLICATION_JSON)
@Transactional
public class PriceEndpoint {
	  private final PriceRepository prices;
	
	  @Autowired
	  public PriceEndpoint(final PriceRepository prices) {
	    this.prices=prices;
	  }
	  
	  @GET
	  public PriceAdapter getLatestPrice() {
		 //Price p=new Price();
		 //p.setPrice(new BigDecimal(3));	
		 
		  PriceAdapter pa=new PriceAdapter();
		 
		 for(Price tmp:prices.findLatestPrice()){
			 if(tmp.getPriceRegion()==PriceRegion.EU){
				 pa.setPrice_zone1(tmp.getPrice().doubleValue());
			 }else if(tmp.getPriceRegion()==PriceRegion.NONEU){
				 pa.setPrice_zone2(tmp.getPrice().doubleValue());
			 }else{  //undefined
				 pa.setPrice_zone3(tmp.getPrice().doubleValue());
			 }
		 }
		 	 
		 
	     return Endpoint.failIfNull(pa, "no price found");
	  }
	  
	  /*
	  @POST
	  public Response create(@Context UriInfo uri,@FormParam("price") double price,@FormParam("region") PriceRegion region) {
	    Price created = new Price();
	    created.setPrice(new BigDecimal(price));
	    created.setPriceRegion(PriceRegion.EU);
	    created = prices.save(created);
	    return Response.created(uri.getAbsolutePathBuilder().path(created.getId().toString()).build()).build();
	  }*/
	  
	  @POST
	  @Path("/set")
	  public Response create(PriceAdapter pa) {
	    Price created = new Price();
	    created.setPrice(new BigDecimal(pa.getPrice_zone1()));
	    created.setPriceRegion(PriceRegion.EU);
	    created = prices.save(created);
	    
	    created = new Price();
	    created.setPrice(new BigDecimal(pa.getPrice_zone2()));
	    created.setPriceRegion(PriceRegion.NONEU);
	    created = prices.save(created);
	    
	    created = new Price();
	    created.setPrice(new BigDecimal(pa.getPrice_zone3()));
	    created.setPriceRegion(PriceRegion.UNDEF);
	    created = prices.save(created);
	    
	    
	    return Response.ok(Message.info("updated prices")).build();
	  }
}
